<template>
  <div class="total-box">
    <!-- 左侧树结构 -->
    <div class="head-container">
      <el-tree
        ref="tree"
        style="height: 100%; width: 100%"
        :data="deptOptions"
        :props="defaultProps"
        :expand-on-click-node="false"
        highlight-current
        node-key="id"
        :default-expanded-keys="defaultExpandedKeys"
        @node-click="handleNodeClick"
      >
        <template slot-scope="{node,data}">
          <span class="el-tree-node_box" :title="data.name">{{ data.name }}</span>
        </template>
      </el-tree>
    </div>

    <!-- 右侧 -->
    <div class="app-container">
      <el-row :gutter="20" style="height: 100%">
        <!--用户数据-->
        <el-col :span="24" :xs="24">
          <HkSearchform
            :data="queryParams"
            :field-list="filterInfo.fieldList"
            :list-type-info="dictList"
            @handleSearch="handleQuery"
            @handleReset="resetQuery"
            @handleEvent="handleEvent"
          />

          <el-row :gutter="10" style="margin-left:0">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新建</el-button>

            <el-button icon="el-icon-download" size="mini" @click="importTemplate">下载模板</el-button>
            <el-button size="mini" @click="handleImport">导入 </el-button>
            <el-button size="mini" @click="handleExport">导出 </el-button>
          </el-row>

          <hk-table
            ref="tableRef"
            :loading="loading"
            :table-data="tableData"
            :table-columns="tableList"
            :selection="false"
            :show-index="true"
            :total="total"
            :query-params="queryParams"
            :operate="true"
            :common-fixed="true"
            device
            secproduct
            :table-height="80"
            :operate-width="200"
            @handleSelectionChange="handleSelectionChange"
            @changePage="getPageList"
          >
            <template #operate="scope">
              <el-button size="mini" type="text" @click="handleUpdate(scope.row)">修改 </el-button>
              <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除 </el-button>
            </template>
          </hk-table>
        </el-col>
      </el-row>
    </div>
    <!-- 新增和编辑的对话框 -->
    <EditDialog ref="editDialogRef" :title="title" :dict-list="dictList" @handleAdd="getList" />
    <!-- 输出 -->
    <Upload ref="import_dia" :url="importUrl" @submit="getList" />
  </div>
</template>

<script>
import { entityTree } from '@/api/assets/netDevice.js'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import HkTable from '@/components/Hk-Table/index.vue'
import EditDialog from './component/emitDialog.vue'
import { deleteDatas, listAll, queryPageList } from '@/api/assets/netDevice.js'
import Upload from '../../components/Upload'
import HkSearchform from '@/components/HkSearchform/index.vue'
import DictLabel from '@/components/Common/dictLabel.vue'

export default {
  components: {
    Treeselect,
    HkTable,
    EditDialog,
    Upload,
    HkSearchform,
    DictLabel
  },
  data() {
    return {
      defaultExpandedKeys: [],
      // 搜索项配置
      filterInfo: {
        fieldList: [
          { label: '关键字', type: 'input', field: 'keyword' },
          {
            label: '设备类型 ',
            type: 'select',
            field: 'equipmentType',
            list: 'equipmentNetTypeList'
          },
          {
            label: '设备状态',
            type: 'select',
            field: 'equipmentState',
            list: 'equipmentStatusList'
          }
        ]
      },
      importUrl: window.baseUrl.asset + '/assetsNetEquipment/import',
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 表格数据
      tableData: [{}],
      //  表格
      tableList: [
        { label: '设备名称', param: 'name', width: '130px' },
        { label: '设备IP', param: 'equipmentIp', width: '130px' },
        { label: '设备MAC', param: 'equipmentMac', width: '150px' },
        { label: '设备类型', param: 'equipmentTypeDictLable', width: '130px' },
        { label: '联网类型', param: 'equipmentNetTypeDictLable', width: '130px' },
        { label: '责任人', param: 'equipmentUserName', width: '130px' },
        { label: '责任人部门', param: 'equipmentOrgIdDictLable', width: '340px' },
        { label: '设备状态', param: 'equipmentStatusDictLable' },
        { label: '设备密级', param: 'equipmentSecLevelDictLable' },
        { label: '重要程度', param: 'equipmentCriticalityLevelDictLable', width: '130px' },
        { label: '是否重点关注', param: 'equipmentFocusLevelDictLable', width: '130px' }
      ],
      // 弹出层标题
      title: '',
      // 部门树选项
      deptOptions: undefined,
      // 树结构参数
      defaultProps: {
        children: 'children',
        label: 'name'
      },

      // 查询参数
      queryParams: {
        size: 10,
        current: 1
      },
      // 列表规则
      dictList: {
        // 设备类型
        equipmentNetTypeList: [],
        // 是否
        equipmentLocalizedList: [],

        // 设备类别
        equipmentSerCategoryList: [],

        // 联网类型
        equipmentHostNetTypeList: [],

        // 设备状态
        equipmentStatusList: [],

        equipmentUseStatusList: [],

        // 涉密等级
        equipmentSecLevelList: [],

        // 重要程度
        assetsSecDominList: [],

        // 所在安全域
        equipmentCriticalityLevelList: [],

        // 操作系统名称
        osTypeList: [],

        assetsSecDomainAutoList: [],

        equipmentSecLevelAdjustList: [],

        equipmentSerPurposeList: [],

        equipmentSerSpecList: [],
        equipmentPurposeList: [],
        // 部门
        orgList: []
      }
    }
  },

  created() {
    this.getDict()
    this.getList()
    this.getTreeselect()
  },
  methods: {
    // 获取所有字典
    getDict() {
      return new Promise((resolve, reject) => {
        listAll(
          'equipmentNetType,equipmentLocalized,equipmentHostNetType,equipmentSecLevelAdjust,' +
            'equipmentStatus,equipmentUseStatus,equipmentSecLevel,assetsSecDomainAuto,' +
            'equipmentCriticalityLevel,osType,equipmentSerPurpose,equipmentSerSpec,equipmentPurpose'
        ).then((res) => {
          for (const key in res) {
            this.dictList[key + 'List'] = res[key].map((item) => {
              return {
                value: item.itemVale,
                label: item.itemLabel
              }
            })
          }
          resolve(this.dictList)
        })
      })
    },
    /** 查询列表 */
    getList(val) {
      this.loading = true
      this.queryParams = { ...this.queryParams, ...val, scrollToTop: true }

      queryPageList(this.queryParams).then((res) => {
        this.total = res.total
        this.tableData = res.list
        this.loading = false
        this.queryParams.scrollToTop = false
      })
    },
    getPageList(val) {
      this.getList(val)
    },
    /** 查询部门下拉树结构 */
    getTreeselect(id) {
      entityTree(id).then((res) => {
        this.deptOptions = res
        this.dictList.orgList = res
        this.defaultExpandedKeys = [res[0].id]
      })
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getList({ orgId: data.id })
    },

    /** 搜索按钮操作 */
    handleQuery(val) {
      this.queryParams.current = 1
      this.getList()
    },
    // 重置表单
    resetQuery() {
      for (const key in this.queryParams) {
        this.queryParams[key] = undefined
      }
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getList()
    },
    // 搜索框变化
    handleEvent(val) {
      const field = val.field
      const value = val.value
      this.queryParams = {
        ...this.queryParams,
        ...{
          [field]: value
        }
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.title = '添加'
      this.$refs.editDialogRef.show()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '编辑'
      this.$refs.editDialogRef.show(row.id)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteDatas(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getList()
        })
        .catch((error) => {
          console.log(error)
        })
    },

    // 修改状态
    handleStatusChange(row) {},
    /** 导出按钮操作 */
    handleExport() {
      this.gDownload(
        window.baseUrl.asset + '/assetsNetEquipment/export',
        {
          ...this.queryParams
        },
        `网络设备信息_${new Date().getTime()}.xlsx`
      )
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import_dia.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.gDownload(window.baseUrl.asset + '/assetsNetEquipment/exportTemplate', {}, `网络设备导入模版.xlsx`)
    }
  }
}
</script>
<style lang="scss" scoped>
.total-box {
  font-size: 14px;
  display: flex;
  width: 100%;
  height: 100%;
}
.head-container {
  overflow: auto;
  padding: 20px;
  // height: 100%;
  width: 270px;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
}
.app-container {
  flex: 1;
  overflow-y: auto;
}
.el-col {
  height: 100%;
  display: flex;
  flex-direction: column;
  &::v-deep .targetTable {
    flex: 1;
    overflow: auto;
  }
}
</style>
